//
//  NSObject+YJEmpty.m
//  Rail
//
//  Created by hychen on 2018/2/26.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "NSObject+YJEmpty.h"

@implementation NSObject (YJEmpty)
- (BOOL)objectIsNotEmpty
{
    if ([self isKindOfClass:[NSString class]]) {
        return ![(NSString *)self isEqualToString:@""];
    }else if([self isKindOfClass:[NSNumber class]])
    {
        return ![(NSNumber *)self isEqual:[NSNumber numberWithInt:0]];
    }else if([self isKindOfClass:[NSDictionary class]])
    {
        return !([[(NSDictionary *)self allKeys] count] == 0);
    }else if([self isKindOfClass:[NSArray class]])
    {
        return !([(NSArray *)self count] == 0);
    }
    return NO;
}

//强制转换成字符串
- (NSString *)forceToNSString
{
    if ([self isKindOfClass:[NSString class]]) {
        return (NSString *)self;
    }else if([self isKindOfClass:[NSNumber class]])
    {
        return [NSString stringWithFormat:@"%@",(NSNumber *)self];
    }
    return @"";
}

//强制转换成数组
- (NSArray *)forceToNSArray
{
    if ([self isKindOfClass:[NSArray class]]) {
        return (NSArray *)self;
    }
    return [NSArray array];
}

//强制转换成字典
- (NSDictionary *)forceToNSDictionary
{
    if ([self isKindOfClass:[NSDictionary class]]) {
        return (NSDictionary *)self;
    }
    return [NSDictionary dictionary];
}

//强制转换成数字对象
- (NSNumber *)forceToNSNumber
{
    if ([self isKindOfClass:[NSNumber class]]) {
        return (NSNumber *)self;
    }else if([self isKindOfClass:[NSString class]])
    {
        return [NSNumber numberWithInteger:[(NSString *)self integerValue]];
    }
    return [NSNumber numberWithInteger:0];
}
//强制转换成集合
- (NSSet *)forceToNSSet
{
    if ([self isKindOfClass:[NSSet class]]) {
        return (NSSet *)self;
    }
    return [NSSet set];
}
//安全的objectForKey
- (id)objectForKeySafe:(id)key
{
    if (!key) {
        return nil;
    }
    if ([self isKindOfClass:[NSDictionary class]]) {
        return [(NSDictionary *)self objectForKey:key];
    }
    return nil;
}
//安全的objectAtIndex
- (id)objectAtIndexSafe:(NSInteger)i;
{
    if ([self isKindOfClass:[NSArray class]]) {
        if (i < [(NSArray *)self count]) {
            return [(NSArray *)self objectAtIndex:i];
        }
    }
    return nil;
}
@end
